<?php
namespace app\beautiful\logic\order;
use app\beautiful\logic\FreightLogic;
use app\beautiful\model\mall\FlashSaleGoodsModel;
use app\beautiful\model\mall\FlashSaleGoodsSkuModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\GoodsSkuModel;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\model\order\GoodsOrderItemModel;
use app\beautiful\model\order\GoodsOrderModel;
use app\beautiful\model\setting\SettingBalanceModel;
use app\beautiful\model\setting\SettingIntegralModel;

class OrderFlashSaleLogic{
    use OrderTrait;
    protected $flash_sale_goods_data=[];
    protected $total_price=0;
    protected $member_coupon_id;
    protected $flash_sale_sku_datas=[];
    protected $order_item_data=[];
    protected $num;

    public function createOrder($params)
    {

        //判断用户
        empty($params['member_id']) && $this->error('请先登录后再操作');
        $this->checkMember($params['member_id'], false);

        //判断商品
        if(empty($params['goods']) || !is_array($params))  $this->error('请选择商品');
        foreach ($params['goods'] as $k => $v){
            $this->checkGoods($v,false);
        }


        //判断配送方式
        if( empty($params['logistics_type']) || !in_array($params['logistics_type'],[1,2])) $this->error('请选择配送方式');
        //快速配送判断邮寄地址
        if($params['logistics_type']==1){
            empty($params['receiving_name']) && $this->error('请输入收货人');
            if(empty($params['receiving_mobile']) || (!is_mobile($params['receiving_mobile']) && !is_phone($params['receiving_mobile'])))  $this->error('请输入收货人手机号');
            empty($params['receiving_address_province']) && $this->error('请输入收货省份');
            empty($params['receiving_address_city']) && $this->error('请输入收货城市');
            empty($params['receiving_address_county']) && $this->error('请输入收货城镇');
            empty($params['receiving_address_info']) && $this->error('请输入收货具体地址');
        }

        //判断总价
        if(empty($params['total_price'])) $this->error('参数错误#1');
        if((float)$params['total_price'] != $this->total_price) $this->error('参数错误#2');

        //判断优惠卷
        $coupon_id = $params['coupon_id'] ?? 0;
        $couponBalance = 0;
        if(!empty($coupon_id)){
            if(null === $myCoupon =  MemberCouponModel::find($coupon_id)) $this->error('优惠券不存在');
            if($myCoupon->status == 1 || $myCoupon->money_limit > $this->total_price) $this->error('该优惠券不可用');
            if($myCoupon->member_id != $params['member_id'])  $this->error('该优惠券不可用');
            $couponBalance = $myCoupon->coupon_money;
        }

        //判断积分抵扣
        $use_integral=(int)$params['use_integral'];
        if($use_integral==1){
            $integral_balance=$this->getIntegralBalance($this->total_price);
        }
        $integral=isset($integral_balance) ? $integral_balance['integral'] : 0;
        $balance=isset($integral_balance) ? $integral_balance['balance'] : 0;


        //获取邮费
        $freight = 0;
        if($params['logistics_type']==1){
            $province= empty($params['receiving_address_province']) ? '' : $params['receiving_address_province'];
            $freight_logic= new FreightLogic($this->shopId);
            $freight=$freight_logic->getFreightPrice($province,$this->num);
            if($freight==-1) $this->error('收货地址暂不支持发货');
        }

        //获取需要支付的金额
        $need_pay=round((($this->total_price)*100 + $freight*100 - $couponBalance*100 - $balance*100)/100,2);

        //保存订单
        $order = new GoodsOrderModel();
        $order_data=[
            'shop_id'=>$this->shopId,
            'order_type'=>'flash_sale',
            'logistics_type'=>$params['logistics_type'],
            'member_id'=>$params['member_id'],
            'total_price'=>$this->total_price,
            'need_pay'=>$need_pay,
            'freight'=>$freight,
            'member_coupon_id'=>$params['coupon_id'],
            'coupon_money'=>$couponBalance,
            'use_integral'=>isset($integral_balance) ? $integral_balance['integral'] : 0,
            'integral_balance'=>isset($integral_balance) ? $integral_balance['balance'] : 0,
            'is_paid'=>0,
            'status'=>1,
        ];
        if($params['logistics_type']==1){
            $order_data['receiving_name']=$params['receiving_name'];
            $order_data['receiving_mobile']=$params['receiving_mobile'];
            $order_data['receiving_address_province']=$params['receiving_address_province'];
            $order_data['receiving_address_city']=$params['receiving_address_city'];
            $order_data['receiving_address_county']=$params['receiving_address_county'];
            $order_data['receiving_address_info']=$params['receiving_address_info'];
        }
        $order->save($order_data);

        //保存订单商品
        foreach ($this->order_item_data as $k => $v){
            $this->order_item_data[$k]['order_id']=$order->order_id;
        }
        $order_item_model= new GoodsOrderItemModel();
        $order_item_model->saveAll($this->order_item_data);

        //保存秒杀商品
        $flash_sale_goods_model=new FlashSaleGoodsModel();
        $flash_sale_goods_model->saveAll($this->flash_sale_goods_data);

        //保存商品规格
        $flash_sale_goods_sku_model = new FlashSaleGoodsSkuModel();
        $flash_sale_goods_sku_model->saveAll($this->flash_sale_sku_datas);

        return $order->order_id;
    }

    public function cancelOrder($params)
    {
        $this->checkMember($params['member_id'],false);
        $this->checkOrder($params['order_id'],true);

        if($this->order->order_type != 'flash_sale') $this->error('订单不存在');
        if($this->order->is_delete != 0) $this->error('订单不存在');
        if($this->order->is_paid == 1 || $this->order->status != 1) $this->error('订单无法取消');

        $this->order->status=-1;
        $this->order->save();

        $this->cancelGoods();

        //保存秒杀商品
        $flash_sale_goods_model=new FlashSaleGoodsModel();
        $flash_sale_goods_model->saveAll($this->flash_sale_goods_data);

        //保存商品规格
        $flash_sale_goods_sku_model = new FlashSaleGoodsSkuModel();
        $flash_sale_goods_sku_model->saveAll($this->flash_sale_sku_datas);

        return true;
    }

    public function getIntegralBalance($total_price)
    {
        $SettingBalance = SettingBalanceModel::where([['shop_id','=', $this->shopId]])->find();
        if(null === $SettingBalance){
            $integral_deduction_balance_limit_rate = 0;
        }else{
            $integral_deduction_balance_limit_rate = $SettingBalance->integral_deduction_balance_limit_rate; //积分抵扣的比率 百分比
        }
        $integralSetting = SettingIntegralModel::where([['shop_id','=', $this->shopId]])->find();
        if($integralSetting === null){ //如果是空 代表不使用积分抵扣余额
            $integral_exchange_balance = 0;
        }else{
            $integral_exchange_balance = $integralSetting->integral_exchange_balance;//汇率
        }

        $pre_can_exchange_balance=round(($total_price*($integral_deduction_balance_limit_rate*100))/10000,2);
        $pre_can_exchange_integral=$integral_exchange_balance==0 ? 0 :round(($pre_can_exchange_balance*10000)/($integral_exchange_balance*100),0);
        $member_integral=$this->member->integral;
        if($member_integral>=$pre_can_exchange_integral){
            $return=[
                'integral'=>$pre_can_exchange_integral,
                'balance'=>$pre_can_exchange_balance
            ];
        }else{
            $return=[
                'integral'=>$member_integral,
                'balance'=>round(($member_integral*($integral_exchange_balance*100))/10000,2),
            ];
        }
        return $return;

    }

    public function checkGoods($datas,$lock)
    {
        empty($datas['flash_sale_goods_id']) && $this->error('参数错误#6');
        empty($datas['num']) && $this->error('参数错误#4');
        $this->num = ($this->num ?? 0) + $datas['num'];

        $today=date("Y-m-d",time());
        $flash_sale_goods=FlashSaleGoodsModel::where([
            ['shop_id','=',$this->shopId],
            ['status','=',1],
            ['is_delete','=',0],
            ['flash_sale_goods_id','=',$datas['flash_sale_goods_id']],
            ['quota','>',$datas['num']],
            ['start_time','<=',$today],
            ['end_time','>=',$today],
        ])->field('flash_sale_goods_id,goods_id,flash_sale_price,quota,take_count,fictitious_take_count,is_have_sku')
            ->lock($lock)->find();
        empty($flash_sale_goods) && $this->error('请选择商品');

        $goods=GoodsModel::find($flash_sale_goods->goods_id);
        empty($goods) && $this->error('商品不存在');

       $flash_sale_goods_sku_id=isset($datas['flash_sale_goods_sku_id']) ? $datas['flash_sale_goods_sku_id'] : 0;
       if($flash_sale_goods_sku_id != 0){
           $flash_sale_sku=FlashSaleGoodsSkuModel::where([
               ['shop_id','=',$this->shopId],
               ['is_delete','=',0],
               ['flash_sale_goods_id','=',$datas['flash_sale_goods_id']],
               ['flash_sale_goods_sku_id','=',$flash_sale_goods_sku_id],
               ['quota','>',$datas['num']]
           ])->field('flash_sale_goods_sku_id,name,original_price,flash_sale_price,quota,take_count')
               ->lock($lock)->find();
           if(null === $flash_sale_sku) $this->error($goods->name.'的规格缺货');

       }

        //处理订单商品表
        $this->order_item_data[]=[
            'shop_id'=>$this->shopId,
            'goods_id'=>$datas['flash_sale_goods_id'],
            'goods_type'=>'flash_sale',
            'goods_name'=>$goods->name,
            'goods_cover_img'=>$goods->cover_img,
            'goods_sku_id'=>$flash_sale_goods_sku_id,
            'goods_sku_name'=>isset($flash_sale_sku) ? $flash_sale_sku->name : '',
            'goods_original_price'=>isset($flash_sale_sku) ? $flash_sale_sku->original_price : $goods->original_price,
            'goods_price'=>isset($flash_sale_sku) ? $flash_sale_sku->flash_sale_price : $flash_sale_goods->flash_sale_price,
            'goods_num'=>$datas['num'],
        ];

        $price=isset($flash_sale_sku) ?  $flash_sale_sku->flash_sale_price :  $flash_sale_goods->flash_sale_price;
        $this->total_price=round((($this->total_price)*100+$price*$datas['num']*100)/100,2);

       //处理秒杀商品表
        $flash_sale_goods=$flash_sale_goods->toArray();
        $flash_sale_goods['quota']=$flash_sale_goods['quota']-$datas['num'];
        $flash_sale_goods['take_count']=$flash_sale_goods['take_count']+$datas['num'];
        $flash_sale_goods['fictitious_take_count']=$flash_sale_goods['fictitious_take_count']+$datas['num'];
        $this->flash_sale_goods_data[]=$flash_sale_goods;

       //处理规格表
        if(isset($flash_sale_sku)){
            $flash_sale_sku=$flash_sale_sku->toArray();
            $flash_sale_sku['quota']=$flash_sale_sku['quota']-$datas['num'];
            $flash_sale_sku['take_count']=$flash_sale_sku['take_count']+$datas['num'];
            $this->flash_sale_sku_datas[]=$flash_sale_sku;
        }

    }

    protected function cancelGoods()
    {
        $order_item_data=GoodsOrderItemModel::where([
            ['shop_id','=',$this->shopId],
            ['order_id','=',$this->order->order_id],
            ['goods_type','=','falsh_sale']
        ])->field('goods_id,goods_sku_id,goods_num')->select()->toArray();
        $goods_ids=[];$sku_ids=[];$order_item_array=[];

        if(!empty($order_item_data)){
            foreach ($order_item_data as $k => $v){
                $goods_ids[]=$v['goods_id'];
                $sku_ids[]=$v['goods_sku_id'];
                $order_item_array['goods'][$v['goods_id']]=$v;
                $order_item_array['sku'][$v['goods_sku_id']]=$v;
            }
        }

        //处理秒杀商品
        $flash_sale_goods_data=FlashSaleGoodsModel::where([
            ['shop_id','=',$this->shopId],
            ['flash_sale_goods_id','IN',$goods_ids],
        ])->field('flash_sale_goods_id,quota,take_count,fictitious_take_count')->select()->toArray();

        foreach ($flash_sale_goods_data as $k => $v){
            $num=$order_item_array['goods'][$v['flash_sale_goods_id']]['goods_num'] ?? 0;
            $flash_sale_goods_data[$k]['quota']=$v['quota'] +$num;
            $flash_sale_goods_data[$k]['fictitious_take_count']=($v['fictitious_take_count'] -$num) <=0 ? $v['fictitious_take_count'] : ($v['fictitious_take_count'] -$num);
            $flash_sale_goods_data[$k]['take_count']=$v['take_count'] -$num;
        }
        $this->flash_sale_goods_data=$flash_sale_goods_data;

        //处理规格
        $flash_sale_sku_datas=FlashSaleGoodsSkuModel::where([
            ['shop_id','=',$this->shopId],
            ['flash_sale_goods_sku_id','IN',$sku_ids],
        ])->field('flash_sale_goods_sku_id,quota,take_count')->select()->toArray();

        foreach ($flash_sale_sku_datas as $k => $v){
            $num=$order_item_array['sku'][$v['flash_sale_goods_sku_id']]['goods_num'] ?? 0;
            $flash_sale_sku_datas[$k]['quota']=$v['quota'] +$num;
            $flash_sale_sku_datas[$k]['take_count']=$v['take_count'] -$num;
        }
        $this->flash_sale_sku_datas=$flash_sale_sku_datas;
    }
}